<?php
	
	$a = $_GET['a'];
	$id = $_GET['id'];
	
	if($a == 'dis'){
		include '../common.php';
	}else{
		include '../admin_head.php';
	}

	//删除操作
	if($a == 'del'){
		if($id == '1') exit(admin_jump('!!!禁止删除Admin!!!','2','index.php'));
		$sql = "delete from ".PRE."user where id='$id' ";
		$result = mysql_query($sql);
		if($result && mysql_affected_rows($link) > 0){
			echo admin_jump('删除成功','2','index.php');
		}else{
			echo admin_jump('删除失败','2','index.php');
		}
	}
	
	
	
	//添加用户
	if($a == 'add'){
		$name = $_POST['name'];
		$pwd = $_POST['pwd'];
		$pwd1 = $_POST['pwd1'];
		$email = $_POST['email'];
		$sex = $_POST['sex'];
		$class = $_POST['class'];
		$time = time();
	
		//判断
		if(strlen($name) <= 0) exit(admin_jump('用户名不能为空','2','add.php'));
		if(strlen($pwd) <=0 ) exit(admin_jump('密码不能为空','2','add.php'));
		if($pwd != $pwd1) exit(admin_jump('密码不匹配','2','add.php'));
		$pattern = '/([\w\.\_]{2,10})@(\w{1,}).([a-z]{2,4})/';
		if(!preg_match($pattern, $email))  exit(admin_jump('邮箱格式不正确','2','add.php'));
		
		$pwd = md5($pwd);
		
		$sql = "insert into ".PRE."user(name,password,email,sex,class,reg_time,login_time) values('{$name}','{$pwd}','{$email}','{$sex}','{$class}','{$time}','{$time}')";
		$result = mysql_query($sql);
		if($result && mysql_insert_id() > 0){
			echo admin_jump('添加成功','2','index.php');
		}else{
			echo admin_jump('添加失败','2','add.php');
		}
	}
	
	
	//重置用户密码
	if($a == 'resetpwd'){
		$pwd= md5('password');
		$sql = "update ".PRE."user set password='{$pwd}' where id='{$id}'";
		$result =mysql_query($sql);
		//echo $sql;exit();
		echo admin_jump('重置成功','2','index.php');
	}
	
	//编辑用户
	if($a == 'edit'){
		$name = $_POST['name'];
		$email = $_POST['email'];
		$jifen = $_POST['jifen'];
		$sex = $_POST['sex'];
		$class = $_POST['class'];
		
		$sql = "update ".PRE."user set name='{$name}',email='{$email}',jifen='{$jifen}',sex='{$sex}',class='{$class}' where id='{$id}' ";
		$result = mysql_query($sql);
		if($result && mysql_affected_rows()>0){
			echo admin_jump('更新成功','2','index.php');
		}else{
			echo admin_jump('更新失败','2','index.php');
		}
	}


	if($a == 'dis'){
		$value = $_GET['value'];
		$sql = "update ".PRE."user set disable='{$value}' where id='{$id}'";
		mysql_query($sql);
		header('Location:'.$_SERVER['HTTP_REFERER']);	
	}
	
	
include '../admin_footer.php';
?>
